//
//  ViewController05.swift
//  APPTestSwift
//
//  Created by hiking on 2020/2/23.
//  Copyright © 2020 hiking. All rights reserved.
//

import UIKit

class ViewController05: UIViewController{
   
    
    let CellID01 = "CellID01"
    let CellID02 = "CellID02"
    let CellID03 = "CellID03"
    let HeaderID = "HeaderID"
    
    lazy var collectionView:UICollectionView = {
        //注意虽然我们的layout设置时在代理中，我们也要创建一个layout否则会爆这个错  -> 'UICollectionView must be initialized with a non-nil layout parameter'
        //注意这里的layout是UICollectionViewFlowLayout 而不熟 UICollectionViewLayout，因为我们实现的是UICollectionViewFlowLayout的代理方法(UICollectionViewDelegateFlowLayout)
        let layout = UICollectionViewFlowLayout.init()
        let collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear
        //注册cell
        collectionView.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: CellID01)
        collectionView.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: CellID02)
        collectionView.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: CellID03)
        //注册头部
        collectionView.register(Demo05HeaderView.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: HeaderID)
        
        //设置数据源代理
        collectionView.dataSource = self
        collectionView.delegate = self
        return collectionView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(self.collectionView)
        
    }
}
//pragma mark 数据源代理方法
extension ViewController05: UICollectionViewDataSource {
    //有多少的分组
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 3
    }
    //每组有多少个item
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        }else if section == 1{
            return 5
        }else{
            return 10
        }
    }
    //
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 0{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellID01, for: indexPath)
            cell.backgroundColor = UIColor.systemGreen
            return cell
        }else if indexPath.section == 1{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellID02, for: indexPath)
            cell.backgroundColor = UIColor.systemRed
            return cell
        }else{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellID03, for: indexPath)
            cell.backgroundColor = UIColor.systemBlue
            return cell
        }
    }
    
    //头部
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader {
            let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: HeaderID, for: indexPath) as! Demo05HeaderView
            view.titleLabel.text = "第\(indexPath.section)组"
            return view
        }else{
            fatalError("No Such kind")
        }
    }
}

extension ViewController05: UICollectionViewDelegateFlowLayout{
    //设置头部的尺寸
       func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
             return CGSize.init(width: self.view.bounds.width, height: 35)
       }
       
       //itemSize
       func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
           switch indexPath.section {
           case 0:
               return CGSize.init(width: self.view.bounds.width, height: 150)
           case 1:
               return CGSize.init(width: (self.view.bounds.width - 30) / 2, height: 45)
           default:
               return CGSize.init(width: (self.view.bounds.width - 40) / 3, height: 25)
           }
       }
       // 设置 minimumLineSpacing   水平间隔
       func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
           switch section {
           case 0:
               return 1
           case 1:
               return 2
           default:
               return 5
           }
       }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
           return 5
       }
}

   
